<?php
/**
*
*	缓存函数库 @zairwolf
*/

if(!defined('IN')) exit('Access Denied');


function _cache($cacheName, $cacheTime = 86400, $prefix = 'cache_'){
	$f = ROOT.'data/cache/'.$prefix.$cacheName.'.php';

	if(!is_file($f) || $cacheTime && TIMESTAMP - filemtime($f) > $cacheTime) _cache_update($cacheName, '', $prefix);

	return $f;
}

function _cache_update($cacheName, $cacheData = '', $prefix = 'cache_'){
	$cacheFile = ROOT.'data/cache/'.$prefix.$cacheName.'.php';
	if(!$cacheData) $cacheData = _cache_generate($cacheName);
	rfow($cacheFile, '<'."?php\nif(!defined('IN')) exit('Access Denied');\n".$cacheData);
}

//根据名称生成缓存
function _cache_generate($cacheName){
	global $_CACHE;
	//公告缓存
	if($cacheName=='announce'){
		global $_SYSTEM;
		return "\$_CACHE['$cacheName'] = ".var_export(db_rows("select id, title, content, dateline from rd8_announce order by id DESC limit ".$_SYSTEM['DISPLAY']['announce']), true).';';

	//自定义缓存
	}elseif($cacheName=='cache'){
		$rows=db_rows("select * from rd8_cache where active=1");
		$cache = array();
		foreach($rows as $row) $cache['custom_'.$row['id']] = $row;
		return "\$_CACHE['$cacheName'] = ".var_export($cache, true).';';

	//api接口缓存
	}elseif($cacheName=='api'){
		$apis=db_rows("select address, code, charset, gate + 0 AS gate from rd8_api where active=1");
		foreach($apis as $k => $api) $apis[$k]['gate'] = $api['gate'] == 1 ? 'Dz' : 'Pw';
		return "\$_CACHE['$cacheName'] = ".var_export($apis, true).';';

	//custom自定义缓存 (之所以不include _cache('cache')是因为在调用本部分前已调用)
	}elseif(!empty($_CACHE['cache']) && array_key_exists($cacheName, $_CACHE['cache'])){
		if(!isset($_CACHE['cache'][$cacheName])) exit('unknown custom cache');

		$sql = $_CACHE['cache'][$cacheName]['csql'];
		//安全检查
		if(false !== stripos($sql, 'insert') || false !== stripos($sql, 'delete') || false !== stripos($sql, 'create') || false !== stripos($sql, 'drop') || false !== stripos($sql, 'truncate')) exit('custom safe warning');//false !== stripos($sql, 'update ') || 

		$data = db_rows($sql);
		if($_CACHE['cache'][$cacheName]['twod']) {
			$cacheData = array();
			$with  = explode(',', $_CACHE['cache'][$cacheName]['cwith']);
			$group = $_CACHE['cache'][$cacheName]['cgroup'];
			foreach($data as $row) {
				foreach($with as $v) {
					if(!$v) continue;
					$cacheData[$row[$group]][$v] = $row[$v];
				}
				if(!isset($cacheData[$row[$group]]['custom__data']))
					$cacheData[$row[$group]]['custom__data'] = array();

				$cacheData[$row[$group]]['custom__data'][] = $row;
			}
			$data = array();
			foreach($cacheData as $val) {
				$data[] = $val;
			}
		}
		foreach($data as $key=>$val){
			if(array_key_exists('authorbasic',$val)) $data[$key]['authorbasic_encode']=urlencode($val['authorbasic']);
		}
		return "\$_CACHE['$cacheName'] = ".var_export($data, true).';';//$cacheName中带着custom_前缀，所以不用担心cache重复 @zairwolf 2009-4-15

	//类别缓存'style','state','license'
	}elseif(in_array($cacheName,array('style','state','license'))){
		$data=db_rows("select id, title, description from rd8_class_system where category='$cacheName'");
		$idx=array();
		foreach($data as $v) $idx[$v['id']]=$v['title'];
		return	"\$_CACHE['$cacheName'] = ".var_export($data, true).';'."\n\n".
				"\$_CACHE['IDX']['$cacheName'] = ".var_export($idx, true).';';

	//类别缓存'subject', 'content'
	}elseif(in_array($cacheName,array('subject', 'content'))){
		$data=db_rows("select id, title, priority, dateline, active, deltime from rd8_class_novel where category='$cacheName' and active=1");
		$idx=array();
		foreach($data as $v) $idx[$v['id']]=$v['title'];
		return	"\$_CACHE['$cacheName'] = ".var_export($data, true).';'."\n\n".
				"\$_CACHE['IDX']['$cacheName'] = ".var_export($idx, true).';';






	}else{
		exit('cache error');
	}
	
}
